package com.wutianyi.shiro;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import com.wutianyi.module.member.dataobject.MemberDO;
import com.wutianyi.module.services.MemberServices;

/**
 * @author hanjiewu
 *
 */
public class MemberRealm extends AuthorizingRealm {
	
	private MemberServices memberServices;
	
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		
		return null;
	}
	
	/* (non-Javadoc)
	 * @see org.apache.shiro.realm.AuthenticatingRealm#doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken)
	 * 如果没有相应的company_id则也不允许进入后天
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
			throws AuthenticationException {
		
		UsernamePasswordToken upToken = (UsernamePasswordToken) token;
		
		MemberDO member = memberServices.getMemberByName(upToken.getUsername());
		
		if (null != member && StringUtils.isNotBlank(member.getCompanyId())) {
			return new SimpleAuthenticationInfo(member, member.getPassword(), getName());
		}
		
		return null;
	}
	
	public MemberServices getMemberServices() {
		return memberServices;
	}
	
	public void setMemberServices(MemberServices memberServices) {
		this.memberServices = memberServices;
	}
	
}
